package c.x.jy.wexin.controller;

import c.x.jy.common.annotation.Login;
import c.x.jy.common.utils.PageUtils;
import c.x.jy.common.utils.R;
import c.x.jy.dao.dto.ClubEntityDto;
import c.x.jy.dao.entity.ClubEntity;
import c.x.jy.service.service.ClubService;
import c.x.jy.service.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 
 *
 * @author amwhyh
 * @email xxx@xx.com
 * @date 2021-07-12 10:30:08
 */
@RestController
@RequestMapping("wechat/club")
public class ClubController {
    @Autowired
    private ClubService clubService;
    @Autowired
    private MemberService memberService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = clubService.queryPage(params);

        return R.put(page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
//		ClubEntity club = clubService.getById(id);
        ClubEntityDto club = clubService.getDtoById(id);
        return R.put(club);
    }

    /**
     * 保存
     */
    @Login
    @RequestMapping("/save")
    public R save(@RequestBody ClubEntity club){
        club.setMemberId(memberService.getCurrUser().getId());
		clubService.save(club);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody ClubEntity club){
		clubService.updateById(club);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete/{id}")
    public R delete(@PathVariable("id") Long id){
		clubService.removeByIds(Arrays.asList(id));

        return R.ok();
    }
    /**
     * 增加浏览量
     */
    @Login
    @RequestMapping("/addviewcount")
    public R addviewcount(@RequestBody ClubEntity club){
        if(club!=null)
        club = clubService.getById(club.getId());
        else
            return R.error("错误的动态");
        if(club.getViewTimes()!=null){
            club.setViewTimes(club.getViewTimes()+1);
        }else{
            club.setViewTimes(1);
        }

        clubService.updateById(club);

//        return R.ok();
        return R.put(club);
    }
    /**
     * 增加分享次数
     */
    @Login
    @RequestMapping("/addsharecount")
    public R addsharecount(@RequestBody ClubEntity club){
        if(club!=null)
            club = clubService.getById(club.getId());
        else
            return R.error("错误的动态");
        if(club!=null){
            club.setShareTimes(club.getShareTimes()+1);
        }else{
            club.setShareTimes(1);
        }

        clubService.updateById(club);

//        return R.ok();
        return R.put(club);
    }
}

